package common.server.handler.outside;

import common.server.entity.OutsideResponseMessage;
import common.server.exception.BaseException;
import common.server.respcode.RespCodeService;
import common.server.respcode.RespHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;

/**
 * 全局rest请求异常拦截器
 * 
 * @author Administrator
 * @version [版本号, 2017/11/15 0015]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@ControllerAdvice(annotations = RestController.class)
public class RestExceptionHandler {
	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory
			.getLogger(RestExceptionHandler.class);



	/**
	 * 构建异常返回
	 *
	 * @param e
	 * @param <T>
	 * @return
	 */
	@ExceptionHandler(Exception.class)
	@ResponseBody
	private <T> OutsideResponseMessage<T> runtimeExceptionHandler(Exception e) {
		logger.error("---------> runtimeExceptionHandler error!", e);
		return RespHelper.buildOutsideRespMessage(RespCodeService.COMM_ERROR,null);
	}


	@ExceptionHandler(BaseException.class)
	@ResponseBody
	private <T> OutsideResponseMessage<T> hljBaseExceptionHandler(BaseException e) {
		logger.error("---------> BaseExceptionHandler error!", e);
		return RespHelper.buildOutsideRespMessage(e);
	}

	/**
	 * 构建异常返回
	 * 
	 * @param e
	 * @param <T>
	 * @return
	 */
	@ExceptionHandler(MethodArgumentNotValidException.class)
	@ResponseBody
	private <T> OutsideResponseMessage<T> illegalParamsExceptionHandler(
			MethodArgumentNotValidException e) {
		logger.error("---------> illegalParamsExceptionHandler request!", e);
		return RespHelper.buildOutsideRespMessage(RespCodeService.COMM_ERROR,null);
	}



}
